package com.delio.client.produccion.recetas;

import java.util.ArrayList;
import java.util.List;

import com.delio.client.idioma.DelioConstants;
import com.delio.client.produccion.detallerecetas.DetalleRecetas;
import com.extjs.gxt.ui.client.Style.HorizontalAlignment;
import com.extjs.gxt.ui.client.Style.LayoutRegion;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.util.Margins;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ButtonBar;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter;
import com.extjs.gxt.ui.client.widget.layout.BorderLayout;
import com.extjs.gxt.ui.client.widget.layout.BorderLayoutData;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;

public class SearchRecetaListForm extends Dialog{
	private DelioConstants x = GWT.create(DelioConstants.class);
	public Button btnDeleteDetalleReceta,btnGuardar,btnDeleteReceta;
	ToolBar toolBar,toolBar1;
	ContentPanel cpi,cpg;
	BorderLayoutData center,nor,c,cen,cg;
	String codartf;
	private BeanModel selectedModel;
	ModelData selectedModel1;
	Grid<BeanModel> grid;
	Grid<BeanModel> grid1;
	Button btnEnviar;

		
	public SearchRecetaListForm(){
	  setBodyBorder(false);  
	  setHeading("Listado de Recetas");  
	  setWidth(850);  
	  setHeight(550);
	  setIconStyle("images/icons/search_lupa.png");
	  setModal(true);
      setFrame(true);
      setHeaderVisible(true);
	  setLayout(new BorderLayout());

	  Recetas.getPaginate();
	  DetalleRecetas.getPaginate();
	  
	  btnEnviar = new Button("Enviar");
	  btnEnviar.addSelectionListener(new SelectionListener<ButtonEvent>(){
	    public void componentSelected(ButtonEvent ce){
	     /* if(validarIds()){
	    	concatPDP = codPedido.concat("-").concat(codDetPedido);
	    	opf_concatPDP.detpedido.setValue(concatPDP);
	      	hide();
	      }*/
	    }
	   });
	 
	  addButton(btnEnviar);

	    center  = new BorderLayoutData(LayoutRegion.CENTER);
	    center.setMargins(new Margins(0));
	    center.setCollapsible(true);
	    
   
		nor  = new BorderLayoutData(LayoutRegion.NORTH,300);
		nor.setMargins(new Margins(0,3,3,3));
		 
		cpi = new ContentPanel();
		cpi.setHeaderVisible(false);
		/*cpi.setHeading("Listado de Recetas");
		cpi.setIconStyle("images/icons/table.png");*/
		cpi.setLayout(new BorderLayout());
		  
		c = new BorderLayoutData(LayoutRegion.CENTER);
		 		    
		cen   = new BorderLayoutData(LayoutRegion.CENTER);
		cen.setMargins(new Margins(3));
		    
		cpg = new ContentPanel();
		cpg.setHeading("Listado de Insumos de Receta");
		cpg.setIconStyle("images/icons/table.png");
		cpg.setLayout(new BorderLayout());
			
	 	cg = new BorderLayoutData(LayoutRegion.CENTER,100);
		    
	    final PagingToolBar toolBarPag = new PagingToolBar(10);
        toolBarPag.bind(Recetas.loader);
        
        List<ColumnConfig> conf = new ArrayList<ColumnConfig>();
        CheckBoxSelectionModel<BeanModel> sm = new CheckBoxSelectionModel<BeanModel>();
    
        conf.add(sm.getColumn()); 
        ColumnConfig col  = new ColumnConfig("artdescripcion",x.articulos(),100);
	    conf.add(col);
	    col  = new ColumnConfig("cantidad",x.cantidad(),100);
	    conf.add(col);
	    
	    final ColumnModel cm = new ColumnModel(conf);
	    
	    grid = new Grid<BeanModel>(Recetas.store, cm);  
        grid.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {  
          public void handleEvent(GridEvent<BeanModel> be) {  
        	  Recetas.loader.load(0, 10);  
          }  
        }); 
        sm.setSelectionMode(SelectionMode.SINGLE);
        grid.setSelectionModel(sm);
        grid.getSelectionModel().addListener(Events.SelectionChange,
           new Listener<SelectionChangedEvent<BeanModel>>() {
			@Override
			public void handleEvent(SelectionChangedEvent<BeanModel> be) {		
         	   if(be.getSelection().size() == 1) {
	            // formbinding.bind((BeanModel) be.getSelection().get(0));
	             selectedModel = be.getSelectedItem();
	             DetalleRecetas.receta_id = Integer.parseInt(selectedModel.get("id").toString());
	             DetalleRecetas.loader.load();
	             btnDeleteReceta.setEnabled(true); 
	           }
    		   if(be.getSelection().size()==0){
    			 DetalleRecetas.store.removeAll();
    			 btnDeleteReceta.setEnabled(false);
    	       // formbinding.unbind();
               }
    			if(be.getSelection().size()>1){
       			 btnDeleteReceta.setEnabled(false);
    			// formbinding.unbind();
    		   }
			}
          });
        
        grid.setLoadMask(true);  
        grid.setBorders(true);       
        GridFilters filters = new GridFilters();
        StringFilter artdescripcionFilter = new StringFilter("artdescripcion");        
        StringFilter cantidadFilter = new StringFilter("cantidad");
          
        filters.addFilter(artdescripcionFilter);        
        filters.addFilter(cantidadFilter);        
         
        grid.addPlugin(filters);                
        grid.addPlugin(sm);
           
	    final PagingToolBar toolBarPag1 = new PagingToolBar(10);
        toolBarPag1.bind(DetalleRecetas.loader);

        List<ColumnConfig> conf1 = new ArrayList<ColumnConfig>();
        CheckBoxSelectionModel<BeanModel> sm1 = new CheckBoxSelectionModel<BeanModel>();
    
        conf1.add(sm1.getColumn()); 
        ColumnConfig col1  = new ColumnConfig("artdescripcion",x.articulos(),100);
	    conf1.add(col1);
	    col1  = new ColumnConfig("cantidad",x.cantidad(),100);
	    conf1.add(col1);
	   
	    final ColumnModel cm1 = new ColumnModel(conf1);
	    grid1 = new Grid<BeanModel>(DetalleRecetas.store, cm1);  
        grid1.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {  
          public void handleEvent(GridEvent<BeanModel> be) {  
        	  DetalleRecetas.loader.load(0, 10);  
          }  
        }); 
        sm1.setSelectionMode(SelectionMode.SINGLE);
        grid1.setSelectionModel(sm1);
        grid1.getSelectionModel().addListener(Events.SelectionChange,
           new Listener<SelectionChangedEvent<ModelData>>() {
			@Override
			public void handleEvent(SelectionChangedEvent<ModelData> be) {		
			   if(be.getSelection().size()==0){
				   btnDeleteDetalleReceta.setEnabled(false);
				   DetalleRecetas.store.removeAll();
				 }
			   if(be.getSelection().size()==1){
				 
	  	           selectedModel1 = be.getSelectedItem();
				   btnDeleteDetalleReceta.setEnabled(true);
		           DetalleRecetas.detallereceta_id = selectedModel1.get("id");
		          }
    		   if(be.getSelection().size()>1)
    			   btnDeleteDetalleReceta.setEnabled(false);

			}
          });
        
        grid1.setLoadMask(true);  
        grid1.setBorders(true);       
        GridFilters filt    = new GridFilters();
        StringFilter artdesc  = new StringFilter("artdescripcion");        
        StringFilter artcant = new StringFilter("cantidad");
          
        filt.addFilter(artdesc);        
        filt.addFilter(artcant);        
         
        grid1.addPlugin(filt);                
        grid1.addPlugin(sm1);
      
        cpi.setTopComponent(toolbar());
        cpi.add(grid,c);
		cpi.setBottomComponent(toolBarPag);		
   
		cpg.setTopComponent(toolbar1());
		cpg.add(grid1,cg);
	    cpg.setBottomComponent(toolBarPag1);
	
		add(cpi,nor);
		add(cpg,cen);

		    
	}
	   
	    
	   public ToolBar toolbar(){
		  toolBar = new ToolBar(); 
	      btnDeleteReceta = new Button("Eliminar", IconHelper.createPath("images/icons/delete.gif"));
	      btnDeleteReceta.setEnabled(false);
	      toolBar.add(btnDeleteReceta);
	      btnDeleteReceta.addSelectionListener(new SelectionListener<ButtonEvent>(){
	   	  public void componentSelected(ButtonEvent ce){

		      Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
			   public void handleEvent(MessageBoxEvent be) {
	            if(be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
			       // Recetas.eliminar(Integer.parseInt(id.getValue()));
				    Recetas.loader.load();
		         }
	            }
		      };
		       String msg = "Esta seguro que desea eliminar la Receta?";
		       MessageBox.confirm(x.eliminarCliente(), msg, callback);

			 }
		  });
	     return toolBar;
	    }
	   
		  public ToolBar toolbar1(){
			toolBar1 = new ToolBar();
			btnDeleteDetalleReceta = new Button("Eliminar",IconHelper.createPath("images/icons/delete.gif"));
			btnDeleteDetalleReceta.setEnabled(false);
			toolBar1.add(btnDeleteDetalleReceta);
			btnDeleteDetalleReceta.addSelectionListener(new SelectionListener<ButtonEvent>(){
			  public void componentSelected(ButtonEvent ce){
				    Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
					  public void handleEvent(MessageBoxEvent be) {
			           if (be.getButtonClicked().getItemId().equals(Dialog.YES)) { 
					        DetalleRecetas.eliminar(DetalleRecetas.detallereceta_id);
						    DetalleRecetas.loader.load();
				         }
			           }
				     };
				     String msg = "Esta seguro que desea eliminar Insumo?";
				     MessageBox.confirm("Eliminar Detealle Receta?", msg, callback);
				 }
			   });
		   return toolBar1;
		 }
		
		public ButtonBar buttonForm(){
		  ButtonBar bb = new ButtonBar();
		  btnGuardar = new Button("Guardar",IconHelper.createPath("images/icons/save_all.png"));		  
		  btnGuardar.addSelectionListener(new SelectionListener<ButtonEvent>(){
			public void componentSelected(ButtonEvent ve){
	 
		       final Receta r = new Receta();
			   // r.setCantidad(cantidad.getValue().doubleValue());
			    
		
			    Recetas.guardar(r);
		        Recetas.loader.load();


			 

			 Listener<MessageBoxEvent> callback = new Listener<MessageBoxEvent>() {	 
			  public void handleEvent(MessageBoxEvent be) {
			    if(be.getButtonClicked().getItemId().equals(Dialog.YES)) {  
			     //  r.setId(Integer.parseInt(id.getValue()));
			       Recetas.actualizar(r);
			       Recetas.loader.load();
				  }
				}
		      };
		      String msg = "Esta seguro que desea Actualizar esta Receta?";
		      MessageBox.confirm("Actualizar Receta?", msg, callback);


		   }
	      });

		  HorizontalAlignment align =  HorizontalAlignment.CENTER;
		  bb.add(btnGuardar);
		  bb.setAlignment(align);
		  return bb;
		  }

	   
	//}
}